//
//  KLDoctorListViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLDoctorListViewController.h"
#import "KLDoctorListTableViewCell.h"
#import "KLToBeDoctorViewController.h"
#import "KLDoctorDetailViewController.h"
@interface KLDoctorListViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@property(nonatomic, strong) KLUserModel                     *userModel;
@end

@implementation KLDoctorListViewController


#pragma mark --vc 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"名医";
    [self configSubView];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无医生" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        //[weakSelf st_rightBarAction:nil];
    }];
    
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendDoctorListRequest];
           [self sendMyInfoRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLDoctorListTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLDoctorListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLDoctorListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    __weak typeof(self) weakSelf =  self;
    [cell.contactButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedContactButtonButtonWithIndexPath:indexPath];
    }];
    cell.model = self.dataSouce[indexPath.section];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KLDoctorModel * model = self.dataSouce[indexPath.section];
    KLDoctorDetailViewController * vc = [KLDoctorDetailViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{

    
    if (self.userModel.is_doctor.integerValue == 3) {
        // [SVProgressHUD showInfoWithStatus:@"您已经是医生了"];
    }else if (self.userModel.is_doctor.integerValue == 2){
        [SVProgressHUD showInfoWithStatus:@"您申请成为医生已经被拒绝，请重新申请"];
        [self.navigationController pushViewController:[KLToBeDoctorViewController new] animated:YES];
    }else if (self.userModel.is_doctor.integerValue == 1){
        [SVProgressHUD showInfoWithStatus:@"您申请成为医生正在审核中,请等待审核"];
    } else{
        [self.navigationController pushViewController:[KLToBeDoctorViewController new] animated:YES];
    }
   

}
- (void)onSlectedContactButtonButtonWithIndexPath:(NSIndexPath*)indexPath{
    KLDoctorModel * model = self.dataSouce[indexPath.section];
    NSString * phone = model.phone ;
    BOOL isSelf =  [[STUserManger defult].phone isEqualToString:phone];
    if (isSelf) {
        [SVProgressHUD showInfoWithStatus:@"这是你自己发布的信息"];
        return;
    }
    KLMessageChatViewController * vc = [[KLMessageChatViewController alloc] initWithConversationChatter:phone conversationType:EMConversationTypeChat];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendDoctorListRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/doctor/doctorlist"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"list"]) {
                                                     NSArray * array = dataDic[@"list"];
                                                     self.dataSouce = [NSMutableArray new];
                                                     for (NSDictionary * modelDic in array) {
                                                         KLDoctorModel * model = [KLDoctorModel mj_objectWithKeyValues:modelDic];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
- (void)sendMyInfoRequest{
    [KLUtils fetch_MineInfoWithHandle:^(KLUserModel *userModel, STError *error) {
        self.userModel = userModel;
        //self.userModel.is_doctor = @(0);
        if (userModel.is_doctor.integerValue != 3) {
            [self st_setRightItemWithTitle:@"成为名医" titleColor:[UIColor whiteColor]];
        }
    }];
}
@end

